use anyhow::Result;
use shared::types::{PermissionType, UserPermission};

pub struct PermissionChecker;

impl PermissionChecker {
    pub fn check_permission(
        user_permissions: &[UserPermission],
        path: &str,
        permission: PermissionType,
    ) -> Result<bool> {
        for perm in user_permissions {
            if perm.permission_type == permission && Self::path_matches(&perm.path_pattern, path) {
                return Ok(perm.granted);
            }
        }
        
        // 默认拒绝
        Ok(false)
    }
    
    fn path_matches(pattern: &str, path: &str) -> bool {
        // 简单的路径匹配，支持通配符
        if pattern.contains('*') {
            Self::wildcard_match(pattern, path)
        } else {
            path.starts_with(pattern)
        }
    }
    
    fn wildcard_match(pattern: &str, path: &str) -> bool {
        // 简单的通配符匹配实现
        // 这里可以实现更复杂的模式匹配
        pattern == "*" || path.starts_with(pattern.trim_end_matches('*'))
    }
}
